package cn.liuguohai.dianwang;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseViewHolder;
import com.yzs.yzsbaseactivitylib.activity.YzsBaseListActivity;
import com.yzs.yzsbaseactivitylib.entity.EventCenter;
import com.yzs.yzsbaseactivitylib.line.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.liuguohai.dianwang.bean.DemoListBean;

/**
 * Created by 刘国海 on 2017/5/24.
 */

public class DeViceListActivity extends YzsBaseListActivity implements
        SwipeRefreshLayout.OnRefreshListener{
    @BindView(R.id.refresh_fg_msg)
    SwipeRefreshLayout refreshLayout;
    /**
     * 所有数据数量
     */
    private static final int TOTAL_COUNTER = 36;
    private int mCurrentCounter = 0;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;

    @Override
    protected void initItemLayout() {

      setLayoutResId(R.layout.layout_item_device);
    }

    @Override
    protected void initSetting() {

        setListType(LINEAR_LAYOUT_MANAGER,true);
        //这个方法不设置就会使用默认值
        setPageSize(10);
        openLoadMore();

    }

    @Override
    protected void MyHolder(BaseViewHolder baseViewHolder, Object o) {

    }

    @Override
    protected void loadingMoreLister() {
        Log.e("loadmore","---");
        refreshLayout.setEnabled(false);
        //这是模拟数据逻辑为了展示各种状态
        if (mAdapter.getData().size() < getPageSize()) {
            mAdapter.loadMoreEnd(true);
        } else {
            if (mCurrentCounter >= TOTAL_COUNTER) {
                mAdapter.loadMoreEnd(true);//true is gone,false is visible
            } else {
                if (isErr) {
                    mAdapter.addData(addData());
                    mCurrentCounter = mAdapter.getData().size();
                    mAdapter.loadMoreComplete();
                } else {
                    isErr = true;
                    Toast.makeText(this, "加载失败", Toast.LENGTH_LONG).show();
                    mAdapter.loadMoreFail();

                }
            }
            refreshLayout.setEnabled(true);
        }

    }

    @Override
    protected void initContentView(Bundle bundle) {
        setContentView(R.layout.activity_device);
     refreshLayout= (SwipeRefreshLayout) findViewById(R.id.refresh_fg_msg);
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected void initLogic() {
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, 1));
        mAdapter.addData(addData());

    }

    @Override
    protected void getBundleExtras(Bundle bundle) {

    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }


    @Override
    public void onRefresh() {
        mAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mAdapter.setNewData(addData());
                isErr = false;
                mCurrentCounter = getPageSize();
                refreshLayout.setRefreshing(false);
                mAdapter.setEnableLoadMore(true);
            }
        }, 1000);
    }

    private List<DemoListBean> addData() {
        List<DemoListBean> list = new ArrayList<>();
        DemoListBean bean;
        for (int i = 0; i < getPageSize(); i++) {
            bean = new DemoListBean();
            bean.setTitle("消息--------" + i);
            list.add(bean);
        }

        return list;
    }

}
